//package com.flowerclient.app.modules.purchase;
//
//import android.os.Bundle;
//import android.support.annotation.Nullable;
//import android.support.constraint.ConstraintLayout;
//import android.support.v7.widget.LinearLayoutManager;
//import android.support.v7.widget.RecyclerView;
//import android.text.Spannable;
//import android.text.SpannableString;
//import android.text.TextUtils;
//import android.text.style.RelativeSizeSpan;
//import android.text.style.StyleSpan;
//import android.util.Log;
//import android.view.View;
//import android.widget.FrameLayout;
//import android.widget.ImageView;
//import android.widget.LinearLayout;
//import android.widget.TextView;
//
//import com.alibaba.android.arouter.facade.annotation.Route;
//import com.alibaba.android.arouter.launcher.ARouter;
//import com.chad.library.adapter.base.BaseQuickAdapter;
//import com.eoner.baselibrary.bean.order.ConfirmOrderData;
//import com.eoner.baselibrary.bean.purchase.PurchaseCartData;
//import com.eoner.baselibrary.bean.purchase.PurchaseCartItemBean;
//import com.eoner.baselib.utils.notify.RxBus;
//import com.eoner.baselibrary.utils.ToastUtil;
//import com.eoner.baselibrary.utils.Utils;
//import com.eoner.configmodule.Config;
//import com.eoner.managerlibrary.router.AroutePath;
//import com.flowerclient.app.BaseActivity;
//import com.flowerclient.app.R;
//import com.flowerclient.app.base.ConfirmDialog;
//import com.flowerclient.app.base.ProgressLoginDialog;
//import com.flowerclient.app.modules.bridge.BridgeActivity;
//import com.flowerclient.app.modules.purchase.adapter.PurchaseShopCartAdapter;
//import com.flowerclient.app.modules.purchase.contract.PurchaseShopCartContract;
//import com.flowerclient.app.modules.purchase.contract.PurchaseShopCartPresenter;
//import com.flowerclient.app.modules.purchase.widget.PurchaseCartNumDialog;
//import com.flowerclient.app.widget.TitlebarView;
//
//import org.json.JSONException;
//import org.json.JSONObject;
//
//import java.util.ArrayList;
//import java.util.List;
//
//import butterknife.BindView;
//import butterknife.ButterKnife;
//import butterknife.OnClick;
//import io.reactivex.functions.Consumer;
//
///**
// * @auther hjian
// * @date 2020/9/20.
// */
//@Route(path = AroutePath.PURCHASE_SHOP_CART_ACTIVITY)
//public class PurchaseShopCartActivity extends BaseActivity<PurchaseShopCartPresenter> implements PurchaseShopCartContract.View {
//
//    @BindView(R.id.purchase_cart_bar)
//    TitlebarView purchaseCartBar;
//    @BindView(R.id.purchase_cart_notice_text)
//    TextView purchaseCartNoticeText;
//    @BindView(R.id.purchase_cart_notice_icon)
//    ImageView purchaseCartNoticeIcon;
//    @BindView(R.id.purchase_cart_notice_layout)
//    LinearLayout purchaseCartNoticeLayout;
//    @BindView(R.id.purchase_cart_confirm)
//    TextView purchaseCartConfirm;
//    @BindView(R.id.purchase_cart_selected_icon)
//    ImageView purchaseCartSelectedIcon;
//    @BindView(R.id.purchase_cart_selected_layout)
//    FrameLayout purchaseCartSelectedLayout;
//    @BindView(R.id.purchase_cart_price)
//    TextView purchaseCartPrice;
//    @BindView(R.id.purchase_cart_num)
//    TextView purchaseCartNum;
//    @BindView(R.id.purchase_cart_bottom_layout)
//    ConstraintLayout purchaseCartBottomLayout;
//    @BindView(R.id.purchase_cart_recycler)
//    RecyclerView purchaseCartRecycler;
//
//    private View emptyView;
//    private ProgressLoginDialog progressDialog;
//    private PurchaseShopCartAdapter cartAdapter;
//    private PurchaseCartData cartData;
//    private String cart_ids;
//    private List<String> selectedList;
//    private boolean isAllSelected = false;
//    private int clickPos;
//    private RxBus rxBus;
//
//    @Override
//    protected void onCreate(@Nullable Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_purchase_shop_cart);
//        ButterKnife.bind(this);
//        selectedList = new ArrayList<>();
//
//        cart_ids = "";
//        emptyView = View.inflate(this,R.layout.null_collect,null);
//        ((TextView)emptyView.findViewById(R.id.tv_tip)).setText("您的采购清单是空的哦~");
//        cartAdapter = new PurchaseShopCartAdapter();
//        cartAdapter.setEmptyView(emptyView);
//        emptyView.setVisibility(View.GONE);
//        purchaseCartRecycler.setLayoutManager(new LinearLayoutManager(this));
//        purchaseCartRecycler.setItemAnimator(null);
//        cartAdapter.bindToRecyclerView(purchaseCartRecycler);
//
//        Utils.setDin(purchaseCartPrice, this);
//
//        initListener();
//
//
//        rxBus = RxBus.$();
//        rxBus.OnEvent(rxBus.register(Config.SUBMIT_ORDER), new Consumer<Object>() {
//            @Override
//            public void accept(Object o) {
//                if (selectedList != null && selectedList.size()>0){
//                    selectedList.clear();
//                }
//            }
//        });
//    }
//
//
//    private void initListener() {
//
//        purchaseCartBar.setOnViewClick(new TitlebarView.onViewClick() {
//            @Override
//            public void leftClick() {
//                finish();
//            }
//
//            @Override
//            public void rightClick() {
//
//            }
//        });
//
//        cartAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                ARouter.getInstance().build(AroutePath.PURCHASE_PRODUCT_DETAIL_ACTIVITY)
//                        .withString("product_id",cartAdapter.getData().get(position).getProduct_main_id())
//                        .navigation();
//            }
//        });
//
//        cartAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
//            @Override
//            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
//                clickPos = position;
//                PurchaseCartItemBean itemBean = cartAdapter.getData().get(position);
//                switch (view.getId()){
//                    case R.id.item_purchase_cart_selected_layout:
//                        showProgress();
//                        if (selectedList.contains(itemBean.getCart_id())){
//                            selectedList.remove(itemBean.getCart_id());
//                        }else {
//                            selectedList.add(itemBean.getCart_id());
//                        }
//                        isAllSelected = checkAllSelected();
//                        purchaseCartSelectedIcon.setImageResource(isAllSelected?
//                                R.mipmap.dealer_radio_select:R.mipmap.dealer_radio_default);
//                        refreshData();
//                        break;
//                    case R.id.item_purchase_cart_delete:
//                        ConfirmDialog dialog = new ConfirmDialog(PurchaseShopCartActivity.this,"",null,"确定删除此商品吗","取消","确定","#FF6809");
//                        dialog.setOnChooseListerner(new ConfirmDialog.OnChooseListerner() {
//                            @Override
//                            public void cancel() {
//                                dialog.dismiss();
//                            }
//
//                            @Override
//                            public void confirm() {
//                                showProgress();
//                                mPresenter.deleteData(itemBean.getCart_id());
//                                dialog.dismiss();
//                            }
//                        });
//                        dialog.show();
//                        break;
//                    case R.id.item_purchase_cart_plus:
//                        if (Long.parseLong(itemBean.getStock())<=Long.parseLong(itemBean.getBuy_qty())){
//                            ToastUtil.showToast("没有更多库存了");
//                            return;
//                        }
//                        showProgress();
//                        mPresenter.updateData(itemBean.getCart_id(),String.valueOf(Long.parseLong(itemBean.getBuy_qty())+1));
//                        break;
//                    case R.id.item_purchase_cart_reduce:
//                        if (1>=Long.parseLong(itemBean.getBuy_qty())){
//                            ToastUtil.showToast("请至少添加一件哦");
//                            return;
//                        }
//                        showProgress();
//                        mPresenter.updateData(itemBean.getCart_id(),Long.parseLong(itemBean.getBuy_qty())>Long.parseLong(
//                                itemBean.getStock())?itemBean.getStock():String.valueOf(Long.parseLong(itemBean.getBuy_qty())-1));
//                        break;
//                    case R.id.item_purchase_cart_num:
//                        new PurchaseCartNumDialog(PurchaseShopCartActivity.this, itemBean.getBuy_qty(),
//                                Long.parseLong(itemBean.getStock()), new PurchaseCartNumDialog.CompleteClickListener() {
//                            @Override
//                            public void onCompleteClick(long num) {
//                                showProgress();
//                                mPresenter.updateData(itemBean.getCart_id(),String.valueOf(num));
//                            }
//                        }).show();
//                        break;
//                }
//            }
//        });
//
//    }
//
//    private void refreshData(){
//        StringBuffer buffer = new StringBuffer();
//        for (int i = 0; i < selectedList.size(); i++) {
//            buffer.append(selectedList.get(i));
//            if (i!=selectedList.size()-1){
//                buffer.append(",");
//            }
//        }
//        Log.e("======", "refreshData: " + selectedList.size());
//        cart_ids = buffer.toString();
//        mPresenter.getData(cart_ids);
//    }
//
//    @Override
//    protected void onResume() {
//        super.onResume();
//        refreshData();
//    }
//
//    @OnClick({R.id.purchase_cart_notice_icon, R.id.purchase_cart_confirm, R.id.purchase_cart_selected_layout})
//    public void onViewClicked(View view) {
//        switch (view.getId()) {
//            case R.id.purchase_cart_notice_icon:
//                startActivitry(BridgeActivity.class,new String[][]{{"url",cartData.getTop_tips_url()}});
//                break;
//            case R.id.purchase_cart_confirm:
//                if (Long.parseLong(cartData.getCart_num())<1){
//                    ToastUtil.showToast("请至少购买一件哦");
//                    return;
//                }
//                showProgress();
//                mPresenter.confirmOrder(getProductsStr(),"1");
//                break;
//            case R.id.purchase_cart_selected_layout:
//                allSelectedClick();
//                break;
//        }
//    }
//
//    private String getProductsStr() {
//            JSONObject productsStr = new JSONObject();
//            try {
//                if (selectedList.size()>0) {
//                    for (PurchaseCartItemBean product : cartData.getProducts()){
//                        if (selectedList.contains(product.getCart_id())){
//                            productsStr.put(product.getProduct_id(),
//                                    product.getBuy_qty());
//                        }
//                    }
//                }
//            } catch (JSONException e) {
//                e.printStackTrace();
//            }
//            return productsStr.toString();
//    }
//
//    private void allSelectedClick(){
//        showProgress();
//        selectedList.clear();
//        if (!isAllSelected){
//            for (PurchaseCartItemBean product : cartData.getProducts()){
//                selectedList.add(product.getCart_id());
//            }
//        }
//        isAllSelected = !isAllSelected;
//        purchaseCartSelectedIcon.setImageResource(isAllSelected?
//                R.mipmap.dealer_radio_select:R.mipmap.dealer_radio_default);
//        refreshData();
//    }
//
//    private boolean checkAllSelected(){
//        boolean isAllSelect = true;
//        for (PurchaseCartItemBean product : cartData.getProducts()){
//            if (!selectedList.contains(product.getCart_id())){
//                isAllSelect = false;
//                break;
//            }
//        }
//        return isAllSelect;
//    }
//
//
//    @Override
//    public void showData(PurchaseCartData cartData) {
//
//        dismissProgress();
//        this.cartData = cartData;
//        if (TextUtils.isEmpty(cartData.getTop_tips_text())){
//            purchaseCartNoticeLayout.setVisibility(View.GONE);
//        }else {
//            purchaseCartNoticeLayout.setVisibility(View.VISIBLE);
//            purchaseCartNoticeText.setText(cartData.getTop_tips_text());
//        }
//        setPriceStyle(new StringBuffer("¥ ").append(cartData.getConfirm_price()).toString());
//        purchaseCartConfirm.setText(new StringBuffer("选好了(").append(cartData.getCart_num()).append(")").toString());
//        purchaseCartNum.setText(new StringBuffer("共计").append(cartData.getConfirm_num()).append("件").toString());
//        purchaseCartBottomLayout.setVisibility(cartData.getProducts().size()>0?View.VISIBLE:View.GONE);
//        emptyView.setVisibility(cartData.getProducts().size()>0?View.GONE:View.VISIBLE);
//        cartAdapter.setNewData(cartData.getProducts());
//    }
//
//    @Override
//    public void showFailure(String msg) {
//        dismissProgress();
//        ToastUtil.showToast(msg);
//    }
//
//    @Override//修改数目，成功msg是数量
//    public void operationalResult(boolean isSuccess, int type, String msg) {
//        if (isSuccess){
//            if (selectedList.contains(cartData.getProducts().get(clickPos).getCart_id())){
//                if (type == 1){
//                    selectedList.remove(cartData.getProducts().get(clickPos).getCart_id());
//                }
//                refreshData();
//            }else {
//                if (type == 1){
//                    cartData.getProducts().remove(clickPos);
//                    cartAdapter.notifyItemRemoved(clickPos);
//                }else {
//                    cartData.getProducts().get(clickPos).setBuy_qty(msg);
//                    cartAdapter.notifyItemChanged(clickPos);
//                }
//                dismissProgress();
//            }
//        }else {
//            dismissProgress();
//            ToastUtil.showToast(msg);
//        }
//
//    }
//
//    @Override
//    public void confirmOrderSuccess(ConfirmOrderData data) {
//        dismissProgress();
//        ARouter.getInstance().build(AroutePath.ORDER_SUBMIT_ACTIVITY).withString("buy_type", "1").
//                withString("buy_product", getProductsStr()).withString("saleType", "4")
//                .withSerializable("confirm_order_data", data).navigation();
//    }
//
//    private void showProgress(){
//        cartAdapter.setSelectedList(selectedList);
//        if (progressDialog == null){
//            progressDialog = new ProgressLoginDialog(this);
//        }
//        progressDialog.show();
//        progressDialog.setDesc("");
//    }
//
//    private void dismissProgress(){
//        if (progressDialog != null){
//            progressDialog.dismiss();
//        }
//    }
//
//    private void setPriceStyle(String price){
//        SpannableString spannableString = new SpannableString(price);
//        spannableString.setSpan(new RelativeSizeSpan(0.63f), 0, 2, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
//        spannableString.setSpan(new StyleSpan(android.graphics.Typeface.BOLD),2,price.length(),Spannable.SPAN_INCLUSIVE_INCLUSIVE);
//        if (price.contains(".")) {
//            spannableString.setSpan(new RelativeSizeSpan(0.68f), price.indexOf("."), price.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
//        }
//        purchaseCartPrice.setText(spannableString);
//    }
//}
